<template>
  <div class="content">
    <div class="virusNewUp">
      <div class="virusNewUpText">编辑图片案例</div>
      <div class="virusNewUpBut" @click="returnTo()">返回</div>
    </div>
    <div class="virusNewCenter">
      <div class="newTitle">
        <div class="newLeft">图片名称</div>
        <div class="titleRight">
          <input type="text" v-model="virusTitle">
        </div>
      </div>
      <div class="newDescribe">
        <div class="newLeft">图片描述</div>
        <div class="describeRight">
          <textarea cols="30" rows="10" v-model="virusDescribe"></textarea>
        </div>
      </div>
      <div class="newPic">
        <div class="newLeft">上传图片</div>
        <div class="picRight">
          <input type="file" id="file" style="display:none;">                              　　　
          <div class="uploadPic" v-show="isUpLoadPic == true" >
            <img style="width:80px;height:50px;margin-left:35px;margin-top:8px;" src="../../../assets/image/upLoadPic.gif" @click="upLoadPicContent()">
          </div>
          <img class="uploadPic" v-show="picContent == '' && isUpLoadPic == false"  src="../../../assets/image/chooseBunner.png" @click="upLoadPicContent()">　 
          <img class="uploadPic" v-show="picContent != '' && isUpLoadPic == false"  :src="picUrl" @click="upLoadPicContent()">
        </div>
      </div>
    </div>
    <div class="virusNewUp">
      <div class="virusNewUpBut" @click="save()">保存</div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      vid: '',
      vsid: '',
      vname: '',
      virusTitle: '',
      virusDescribe: '',
      picUrl: '',
      picContent: '',
      isUpLoadPic: true,
    };
  },
  components: {},
  computed: {},
  mounted(){
      this.vsid= this.$route.query.vsid;
      this.showData();
  },
  methods: {
    //返回上一页
    returnTo(){
      this.$router.go(-1);
    },
    showData(){
      this.$api.dataBA
        .showVirusPicData(this.vsid)
        .then((res) => {
          console.log(res.data)
          if (res.data.statu == 0){
            this.isUpLoadPic = true;
            this.virusTitle = res.data.data.title
            this.virusDescribe = res.data.data.des
            this.picContent = res.data.data.thumb
            this.vid = res.data.data.vir_id
            setTimeout(()=>{ 
              this.picUrl = 'http://192.168.100.246:9999' + res.data.data.thumb
              this.isUpLoadPic = false
            }, 1000);
          }
        });
    },
    //上传封面图
    upLoadPicContent(){
      document.getElementById("file").click();
      document.getElementById("file").onchange=(e)=>{ 
          var fileNameStr = e.target.files[0];
          console.log(fileNameStr)
          var xhr, formData;      
          this.isUpLoadPic = true;
          xhr = new XMLHttpRequest();
          xhr.withCredentials = false;
          xhr.open('POST', 'http://192.168.100.246:9999/index.php/admin/upload/virus_outline_upload');//第一个参数是请求方式，第二个参数是请求地址，我这里配置的是struts的action，如果是其他（PHP等）的可这样配置：xxx.php               
          xhr.onload =  ()=> {
              var json= JSON.parse(xhr.responseText); 
              if (json.statu !== 0) {
                  alert('图片上传失败');
                  this.isUpLoadPic = false;
                  return;
              }
              if (!json || typeof json.data !== 'string') {
                  alert('图片上传失败');
                  this.isUpLoadPic = false;
                  return;
              }
              this.picContent= json.data;
              setTimeout(()=>{ 
                  this.picUrl= 'http://192.168.100.246:9999' + json.data;
                  this.isUpLoadPic = false;
              }, 1000);
          };
          formData = new FormData();
          formData.append('file', fileNameStr);
          xhr.send(formData);
      }
    },
    save(){
      if(this.virusTitle == ""){
          alert('请填写图片名称');
          return;
      }
      if(this.virusDescribe == ""){
          alert('请填写图片描述');
          return;
      }
      if(this.picContent == ""){
          alert('请上传图片');
          return;
      }
      this.$api.dataBA
        .virusPicAdd({
          "id":this.vsid,
          "vir_id": this.vid,
          "title": this.virusTitle,
          "des": this.virusDescribe,
          "thumb": this.picContent,
          "status": 1
        })
        .then((res) => {
          if (res.data.statu == 0) this.succeedBack();
        });
    },
    succeedBack() {
      this.$router.go(-1);
    },
  }
}
</script>
<style scoped>
.content{
  /* background-color: #CCCCCC; */
  padding: 0 10px;
}
.virusNewUp{
  width: 100%;
  height: 60px;
  font-size: 14px;
}
.virusNewUpText{
  width: 100px;
  height: 40px;
  line-height: 50px;
  color: rgb(134,141,149);
  float: left;
  margin-left: 49px;
}
.virusNewUpBut{
  width: 60px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  float: right;
  margin-top: 15px;
  border-radius: 4px;
  background-color: #3a94fa;
  color: #FFFFFF;
}
.virusNewCenter{
  width: 80%;
  margin: 0 auto;
  font-size: 14px;
}
.newLeft{
  width: 100px;
  height: 40px;
  line-height: 40px;
  color: rgb(0, 80, 178);;
}
.newTitle{
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
  width: 100%;
  height: 40px;
}
.titleRight{
  flex-grow: 1;
	flex-shrink: 1;
}
.titleRight input{
  width: 100%;
  height: 100%;
  line-height: 40px;
  box-sizing: border-box;
  border: 1px solid #CCCCCC;
  border-radius: 4px;
  padding: 0 10px;
  outline: none;
}
.newDescribe{
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
  width: 100%;
  height: 150px;
  margin-top: 20px;
}
.describeRight{
  flex-grow: 1;
	flex-shrink: 1;
}
.describeRight textarea{
  width: 100%;
  height: 100%;
  line-height: 40px;
  box-sizing: border-box;
  border: 1px solid #CCCCCC;
  border-radius: 4px;
  padding: 0 10px;
  outline: none;
}
.newPic{
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
  width: 100%;
  height: 150px;
  margin-top: 20px;
}
.picRight{
  flex-grow: 1;
	flex-shrink: 1;
}
.uploadPic{
  width: 160px;
  height: 100px;
}
</style>